package com.blb.exam.service.impl;

import com.blb.exam.common.entity.PageBean;
import com.blb.exam.common.entity.QuetionSet;
import com.blb.exam.dao.QuetionSetDAO;
import com.blb.exam.service.QuetionSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuetionSetServiceImpl implements QuetionSetService {

    @Autowired
    private QuetionSetDAO quetionSetDAO;

    @Override
    public List<QuetionSet> findAll() {
        return quetionSetDAO.selectAll();
    }

    @Override
    public QuetionSet findById(Long stuId) {
        return quetionSetDAO.selectById(stuId);
    }

    @Override
    public int add(QuetionSet quetionSet) {
        return quetionSetDAO.insert(quetionSet);
    }

    @Override
    public int update(QuetionSet quetionSet) {
        return quetionSetDAO.update(quetionSet);
    }

    @Override
    public int deleteById(int stuId) {
        return quetionSetDAO.deleteById(stuId);
    }

    @Override
    public PageBean<QuetionSet> findPageLike(String title, int current) {
        //获得页面总数
        int dataCount = quetionSetDAO.selectCountLikeTitle(title);
        //计算分页的数量
        int pageCount = dataCount % PageBean.PAGE_SIZE == 0 ? dataCount / PageBean.PAGE_SIZE : dataCount / PageBean.PAGE_SIZE + 1;
        //分页查询数据 current从1开始
        List<QuetionSet> quetionSets = quetionSetDAO.selectLimitLikeTitle(title,(current - 1) * PageBean.PAGE_SIZE,PageBean.PAGE_SIZE);
        PageBean<QuetionSet> pageBean = new PageBean<>();
        pageBean.setCurrent(current);
        pageBean.setData(quetionSets);
        pageBean.setDataCount(dataCount);
        pageBean.setPageCount(pageCount);
        return pageBean;
    }
}
